<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 10/4/14
 * Time: 8:39 PM
 */

namespace Backend\Modules\Video\Controllers;

use Common\library\Common;
use Library\Constant\Constant;
//use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Paginator\Adapter\QueryBuilder as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\Video;
use Backend\Modules\Video\Forms\FormSeachVideo;

class AjaxController extends \BackendController
{
    public function loadVideoAction()
    {
        $this->view->setTemplateAfter('empty');

        $currentPage = abs($this->request->getQuery('page', 'int', 1));

        $video = new Video();
        $videoBuilder = $video->queryBuilderSeachVideo(array());
        $formSearchVideo = new FormSeachVideo();

        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'builder'  => $videoBuilder,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                'rangeLength' => 5,
                'urlMask'     => '',
            )
        );
        $this->view->objectType = $this->request->get('object_type');
        $this->view->objectId = $this->request->get('object_id');
        $this->view->pager = $pager;
        $this->view->formSearchVideo = $formSearchVideo;
        $this->view->render("ajax", "modal/modal_video");
    }

    
    public function seachVideoAction()
    {
        if($this->request->isAjax())
        {
            $this->view->setTemplateAfter('empty');

            $currentPage = abs($this->request->getQuery('page', 'int', 1));


            $video = new Video();
            $videos = $video->queryBuilderSeachVideo($this->request->getPost());

            if ($currentPage == 0) {
                $currentPage = 1;
            }

            $pager = new Pager(
                new Paginator(array(
                    'builder'  => $videos,
                    'limit' => Constant::ITEM_PER_PAGE_20,
                    'page'  => $currentPage,
                )),
                array(
                    'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                    'rangeLength' => 5,
                    'urlMask'     => '',
                )
            );
            $this->view->pager = $pager;
            $this->view->render("ajax", "result_search_video");

            $json['success'] = true;
            $json['html'] = $this->view->getContent();
            $this->response->setContent(json_encode($json));
            //Return the response
            return $this->response->send();
        }
    }
}
